local mod = SexyCooldown2:NewModule("Raid Debuffs", "AceEvent-3.0", "AceBucket-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("SexyCooldown2")

local debuffs =
{
	RAID_DEBUFF_MAJOR_ARMOR =
	{
		113746,		-- Weakened Armor
	},

	RAID_DEBUFF_CAST_SPEED =
	{
		31589,		-- Slow
		73975,		-- Necrotic Strike
		5760,		-- Mind-Numbing Poison
		109466,		-- Curse of Enfeeblement
		50274,		-- Spore Cloud (Hunter pet)
		58604,		-- Lava Breath (Hunter pet)
		90314,		-- Tailspin (Hunter pet)
	},

	RAID_DEBUFF_HEALING =
	{
		82654,		-- Widow Venom
		8680,		-- Wound Poison
		115804,		-- Mortal Wounds
		115625,		-- Mortal Cleave (Warlock pet)
		30213,		-- Legion Strike (Warlock pet)
		54680,		-- Monstrous Bite (Hunter pet)
	},

	RAID_DEBUFF_PHYSICAL_DAMAGE =
	{
		24423,		-- Demoralizing Screech (Hunter pet)
		50256,		-- Demoralizing Roar (Hunter pet)
		1160,		-- Demoralizing Shout
		115798,		-- Weakened Blows
	},

	RAID_DEBUFF_PHYSICAL_VULNERABILITY =
	{
		50518,		-- Ravage (Hunter pet)
		35290,		-- Gore (Hunter pet)
		57386,		-- Stampede (Hunter pet)
		55749,		-- Acid Spit (Hunter pet)
		81326,		-- Physical Vulnerability
	},

	RAID_DEBUFF_SPELL_DAMAGE_TAKEN =
	{
		34889,		-- Fire Breath (Hunter pet)
		24844,		-- Lightning Breath (Hunter pet)
		93068,		-- Master Poisoner
		1490,		-- Curse of the Elements
	}
}

local translatedDebuffs =
{
}

local classes =
{
	RAID_DEBUFF_MAJOR_ARMOR 		= ("|cffe2aa68%s|r"):format(L["Weakened Armor"]),
	RAID_DEBUFF_CAST_SPEED 			= ("|cff68d2e2%s|r"):format(L["Slowed Casting Speed"]),
	RAID_DEBUFF_HEALING 			= ("|cff68d2e2%s|r"):format(L["Mortal Wounds"]),
	RAID_DEBUFF_PHYSICAL_DAMAGE 		= ("|cff68d2e2%s|r"):format(L["Weakened Blows"]),
	RAID_DEBUFF_PHYSICAL_VULNERABILITY 	= ("|cff68d2e2%s|r"):format(L["Physical Vulnerability"]),
	RAID_DEBUFF_SPELL_DAMAGE_TAKEN 		= ("|cff68d2e2%s|r"):format(L["Magical Vulnerability"]),
}

function mod:OnInitialize()
	SexyCooldown2.RegisterFilter(self, "RAID_DEBUFF_MAJOR_ARMOR",
		L["Weakened Armor"],
		L["Show the duration of Weakened Armor debuffs on the target."])

	SexyCooldown2.RegisterFilter(self, "RAID_DEBUFF_CAST_SPEED",
		L["Slowed Casting Speed"],
		L["Show the duration of Slowed Casting Speed debuffs on the target."])

	SexyCooldown2.RegisterFilter(self, "RAID_DEBUFF_HEALING",
		L["Mortal Wounds"],
		L["Show the duration of Healing recive debuffs on the target."])

	SexyCooldown2.RegisterFilter(self, "RAID_DEBUFF_PHYSICAL_DAMAGE",
		L["Weakened Blows"],
		L["Show the duration of Physical Damage Done Reduction debuffs on the target."])

	SexyCooldown2.RegisterFilter(self, "RAID_DEBUFF_PHYSICAL_VULNERABILITY",
		L["Physical Vulnerability"],
		L["Show the duration of Physical Vulnerability debuffs on the target."])

	SexyCooldown2.RegisterFilter(self, "RAID_DEBUFF_SPELL_DAMAGE_TAKEN",
		L["Magical Vulnerability"],
		L["Show the duration of Magical Vulnerability debuffs on the target."])

	for k, v in pairs(debuffs) do
		for _, spellID in ipairs(v) do
			local name, _, icon = GetSpellInfo(spellID)

			if name then
				translatedDebuffs[name] = k
			end
		end
	end
end

function mod:OnEnable()
	self:RegisterBucketEvent("UNIT_AURA", 0.1, "UNIT_AURA")
	self:RegisterEvent("PLAYER_TARGET_CHANGED", "Refresh")
	self:Refresh()
end

function mod:Refresh()
	self:UpdateUnit("target")
end

local function showBuffHyperlink(frame, unit, id, filter, class)
	GameTooltip:SetUnitAura(unit, id, filter)
	GameTooltip:AddLine(class)
end

function mod:UNIT_AURA(units)
	for unit in pairs(units) do
		self:UpdateUnit(unit)
	end
end

do
	local removeBuffs = {}
	local existingBuffs = {}
	local slotDebuffs = {}
	local slotDebuffTimes = {}

	function mod:UpdateUnit(unit)
		if unit ~= "target" then
			return
		end

		wipe(removeBuffs)

		for k, v in pairs(existingBuffs) do
			removeBuffs[k] = v
		end

		wipe(existingBuffs)
		wipe(slotDebuffs)
		wipe(slotDebuffTimes)

		local index = 1
		while true do
			local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit, index, "HARMFUL")

			if not name then
				break
			end

			local s = name -- .. ":" .. icon
			local debuffSlot = translatedDebuffs[s]

			if debuffSlot then
				local uid = debuffSlot .. ":" .. s

				if expirationTime > (slotDebuffTimes[debuffSlot] or 0) then
					if slotDebuffs[debuffSlot] then
						local oldUID = slotDebuffs[debuffSlot]
						existingBuffs[oldUID] = nil
						removeBuffs[oldUID] = true
					end

					slotDebuffTimes[debuffSlot] = expirationTime
					slotDebuffs[debuffSlot] = uid
					SexyCooldown2:AddItem(uid, name, icon, expirationTime - duration, duration, count, debuffSlot, showBuffHyperlink, unit, index, "HARMFUL", classes[debuffSlot])
					existingBuffs[uid] = true
					removeBuffs[uid] = nil
				end
			end

			index = index + 1
		end

		for k, v in pairs(removeBuffs) do
			SexyCooldown2:RemoveItem(k)
		end
	end
end